package com.example.fragment;


import android.animation.Animator;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.inter.LoginCallBack;
import com.example.myapplication.R;
import com.example.util.PaperInfoByIdUtil;
import com.example.util.PaperInfoforUser;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * A simple {@link Fragment} subclass.
 */
public class PaperInfoadviceFragment extends Fragment implements LoginCallBack {

    private TextView text_title1, text_content1;
    private PaperInfoByIdUtil paperInfoByIdUtil;
    private View myView;

    public PaperInfoadviceFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_paper_infoadvice, container, false);
        initView(view);

        paperInfoByIdUtil = new PaperInfoByIdUtil();
        Bundle bundle = getArguments();
        String paper_id = bundle.getString("paper_id");
        paperInfoByIdUtil.getInfo(paper_id, this);

        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        //startAnimation();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public void startAnimation() {
// get the center for the clipping circle
        int cx = (myView.getLeft() + myView.getRight()) / 4;
        int cy = (myView.getTop() + myView.getBottom()) / 4;

// get the final radius for the clipping circle
        int finalRadius = myView.getWidth()/2;

// create and start the animator for this view
// (the start radius is zero)
        Animator anim =
                ViewAnimationUtils.createCircularReveal(myView, cx, cy, 0, finalRadius);
        anim.start();
    }

    public void initView(View view) {

        text_title1 = (TextView) view.findViewById(R.id.text_title1);
        text_content1 = (TextView) view.findViewById(R.id.text_content1);
        myView = view.findViewById(R.id.Linear_paper_info);
    }


    @Override
    public void onSuccess(String info) {
        Log.i("tag", info);
        try {
            JSONObject jsonObject = new JSONObject(info);
            JSONObject jsonObject1 = jsonObject.getJSONObject("info");
            String papername = jsonObject1.getString("paper_name");
            String papercontext = jsonObject1.getString("paper_context");
            text_content1.setText(papercontext);
            text_title1.setText(papername);
            startAnimation();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onFailed() {

    }
}
